#include "MA_IIR_Filter.h"

#define USE_SATURATION
#define	MAX_ABS			30000


MA_IIR_Filter::MA_IIR_Filter(void)
{
	FilterLenAR =	0;
	FilterLenMA	=	0;
	memset(AR,0,sizeof(AR));
	memset(MA,0,sizeof(MA));
	memset(MA_mem,0,sizeof(MA_mem));
	memset(AR_mem,0,sizeof(AR_mem));
	LoadSetting();
}

MA_IIR_Filter::~MA_IIR_Filter(void)
{
}
void	MA_IIR_Filter::LoadSetting(void)
{
	int Lines,i;
	wxTextFile	File;
	FilterNumber	=	0;
	//memset(Filters,0,sizeof(Filters));
	if(File.Open(_T("./Filters/Settings.txt")))
	{
		Lines = (int)File.GetLineCount();
		if( (Lines%2) != 0)
		{
			::wxMessageBox(_T("MA_IIR_FILTER: Wrong Settings File Length"));	
			return;
		}
		FilterNumber = Lines/2;
		for(i=0;Lines>1;Lines-=2,i++)
		{
			Filters[i].FileName = File.GetLine(i*2);
			Filters[i].Name		=	File.GetLine(i*2+1);
		}
		File.Close();
	}else
	{
		::wxMessageBox(_T("MA_IIR_FILTER: Settings File not Found"));	
	}

}
int	MA_IIR_Filter::SelectFilter(int	Kind,wxString *FileName)
{
	FilterLenAR =	0;
	FilterLenMA	=	0;
	memset(AR,0,sizeof(AR));
	memset(MA,0,sizeof(MA));
	memset(MA_mem,0,sizeof(MA_mem));
	memset(AR_mem,0,sizeof(AR_mem));

	if(Kind == -1) return 0;
	if(Kind > FilterNumber) return 0;

	LoadFromFile(Filters[Kind].FileName);

	return 1;
}



void MA_IIR_Filter::FilterSample(HimDataStruct *pData)
{
	int		b,ch;
	float	TempMA;
	float	TempAR;
	float	OutValueY;
	if(FilterLenMA == 0) return;

	for(ch=0;ch < pData->DataLen;ch++)
	{
		MA_mem[ch][0]	=	pData->Signal[ch];
		
		//Calcolo la parte MA
		TempMA = 0;
		for(b=0;b<FilterLenMA;b++)
		{
			TempMA = TempMA + MA[b]* MA_mem[ch][b];
		}

		//Calcolo la parte AR
		TempAR = 0;
		if(FilterLenAR>0)
		{
			memmove(&AR_mem[ch][1],&AR_mem[ch][0],sizeof(AR_mem[0][0])*(FilterLenAR-1));
			for(b=1;b<FilterLenAR;b++)
			{
				TempAR = TempAR + AR[b]*AR_mem[ch][b];
			}
		}
	
		//Riordino i vettori	
		memmove(&MA_mem[ch][1],&MA_mem[ch][0],sizeof(MA_mem[0][0])*(FilterLenMA-1));
		
		if(FilterLenAR>0)
		{
			OutValueY	=	(TempMA-TempAR)/AR[0];
			AR_mem[ch][0]	=	pData->Signal[ch] = OutValueY;
		}else
		{
				OutValueY	=	pData->Signal[ch] = TempMA;		
		}

	}
		
	return;
}

void MA_IIR_Filter::FilterData(HimDataStruct *pData,int Len)
{
	for(;Len >0;Len--,pData++)FilterSample(pData);
}

void	MA_IIR_Filter::LoadFromFile(wxString FileName)
{
	char	Kind;
	unsigned int		TempLen;
	wxFile File;
	
	FilterLenAR =	0;
	FilterLenMA	=	0;
	memset(AR,0,sizeof(AR));
	memset(MA,0,sizeof(MA));
	memset(MA_mem,0,sizeof(MA_mem));
	memset(AR_mem,0,sizeof(AR_mem));
	
	if(File.Open(FileName))
	{
		File.Read(&Kind,sizeof(Kind));
		switch(Kind)
		{
		case 'I':
			File.Read(&TempLen,sizeof(TempLen));
			FilterLenAR	=	TempLen;
			File.Read(AR,sizeof(float)*FilterLenAR);

			File.Read(&TempLen,sizeof(TempLen));
			FilterLenMA	=	TempLen;
			File.Read(MA,sizeof(float)*FilterLenMA);

			break;
		case 'M':
			File.Read(&TempLen,sizeof(TempLen));
			FilterLenMA	=	TempLen;
			File.Read(MA,sizeof(float)*FilterLenMA);
			break;		
		}

	
	}
}